package com.example.demo.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
public class HelloService {
    public  List<String> hello()  {
        //爬取页面数据
        String url = "http://rd-check-static.oss-cn-beijing.aliyuncs.com/ipo.html";
        Document document = null;
        try {
            document = Jsoup.parse(new URL(url), 30000);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //通过正则切出目录
        String regex2 = "[第]([一二三四五六七八九十]{0,2})[节].*|[一二三四五六七八九十]{1,2}[、].*|[（][一二三四五六七八九十]{1,3}[）].*";
        Pattern r2 = Pattern.compile(regex2, Pattern.MULTILINE);
        Elements elements = document.getElementsMatchingOwnText(r2);
        List<String> list2 = new ArrayList<>();

        //将父标签的为<td>的目录放入list
        for (int i = 0; i < elements.size(); i++) {
            if (!"td".equals(elements.get(i).parent().nodeName())) list2.add(elements.get(i).text());
        }
//        list2.stream().limit()
//        List<String> collect = list2.stream().skip(33).collect(Collectors.toList());

        //通过迭代器将有特殊符号的目录移出list
        Iterator<String> iterator2 = list2.iterator();
        while (iterator2.hasNext()){
            Matcher matcher = cut4(iterator2.next());
            if (matcher.find()) iterator2.remove();
        }

        return  list2;
    }

    /**
     * 将通过正则找出带有特殊符号的目录
     * @param s
     * @return
     */
     public Matcher cut4(String s){
         String regex = "[”“，]";
         Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
         Matcher matcher = pattern.matcher(s);
         return matcher;
     }
}